package com.ebt.m.data.bean.clause;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.util.Log;

/**
 * 险种条款 名词解释
 * @author Michael.li
 *
 */
public class ExplainContent
{
	private static final String TAG = "ExplainContent";
	
	private ExplainContent()
	{}
	/**
	 * 名词
	 */
	public String Name;
	/**
	 * 描述
	 */
	public String Description;
	
	public static List<ExplainContent> get(String path)
	{
		List<ExplainContent> list = new ArrayList<ExplainContent>();
		if(path==null){
			return list;
		}
		File file = new File(path);
		if(!file.exists()){
			return list;
		}
		DocumentBuilderFactory factory = null;
		DocumentBuilder builder = null;
		Document document = null;
		InputStream inputStream = null;
		factory = DocumentBuilderFactory.newInstance();
		try {
			builder = factory.newDocumentBuilder();
			inputStream = new FileInputStream(path);
			document = builder.parse(inputStream);
			Element root = document.getDocumentElement();
			NodeList rootNodes = root.getElementsByTagName("explain");
			for (int i = 0, j = rootNodes.getLength(); i < j; i++) {
				Element rootElement = (Element) (rootNodes.item(i));
				ExplainContent item = new ExplainContent();
				item.Name=rootElement.getAttribute("name");
				item.Description=rootElement.getTextContent();
				list.add(item);
			}
			Log.i(TAG, "success parsed");
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} finally {
			try {
				if(inputStream!=null){
					inputStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return list;
	}
}
